package com.doradance.app.mine.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.entity.FreeItemEntity;
import com.doradance.entity.SaveIdEntity;
import com.doradance.entity.SelectCallBack;
import com.example.xlhratingbar_lib.XLHRatingBar;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2018/2/13.
 */

public class MineCollectAdapter extends RecyclerViewBaseAdapter<FreeItemEntity> {
    private static final int MYLIVE_MODE_CHECK = 0;
    int mEditMode = MYLIVE_MODE_CHECK;
    private int selectPosition;
    ArrayList<SaveIdEntity> idList;
    private Boolean isShow=false;


    public MineCollectAdapter(Context con) {
        super(con);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        return new CollectHolder(setAdapterViewItem(parent, R.layout.mine_delete_collect_item));
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder viewHolder,final int position) {
        CollectHolder holder = (CollectHolder) viewHolder;
        FreeItemEntity indexEntity = getData().get(position);


        if(idList==null){
            idList = new ArrayList<>();
            for(int i=0;i<getData().size();i++){
                SaveIdEntity saveIdEntity = new SaveIdEntity();
                saveIdEntity.setSelect(false);
                saveIdEntity.setId(getData().get(i).getId());
                idList.add(saveIdEntity);
//                if(idList.get(i).getSelect()==true){
//                    holder.mIvSelectCollect.setImageResource(R.mipmap.icon_collect_delete);
//                }else{
//                    holder.mIvSelectCollect.setImageResource(R.mipmap.icon_collect_no_delete);
//                }
            }
        }else{
            if(selectPosition == position && isShow==true){
                holder.mIvSelectCollect.setImageResource(R.mipmap.icon_collect_delete);
                getData().get(position).setSelected(true);
            }else{
                holder.mIvSelectCollect.setImageResource(R.mipmap.icon_collect_no_delete);
                getData().get(position).setSelected(false);
            }
//            if(selectPosition == position && isShow==true){
//                holder.mIvSelectCollect.setImageResource(R.mipmap.icon_collect_delete);
//            }else{
//                holder.mIvSelectCollect.setImageResource(R.mipmap.icon_collect_no_delete);
//            }
        }
        if(indexEntity.isSelected){
            holder.mIvSelectCollect.setImageResource(R.mipmap.icon_collect_delete);
        }else{
            holder.mIvSelectCollect.setImageResource(R.mipmap.icon_collect_no_delete);
        }

        if (mEditMode == MYLIVE_MODE_CHECK) {
            holder.mIvSelectCollect.setVisibility(View.GONE);
        } else {
            holder.mIvSelectCollect.setVisibility(View.VISIBLE);
        }
        holder.mIvSelectCollect.setTag(R.id.tag_first,position);
        holder.mIvSelectCollect.setTag(R.id.tag_second,idList);
        holder.mIvSelectCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//            EventBus.getDefault().post(new SelectCallBack(position,idList));
//                int position = (int) v.getTag(R.id.tag_first);
//                ArrayList<SaveIdEntity> idList = (ArrayList<SaveIdEntity>) v.getTag(R.id.tag_second);
            }
        });
        holder.mTvRcmdTitle.setText(indexEntity.getName());
        holder.mTvRcmdFree.setText(indexEntity.getVip_name());
        holder.mTvRcmdTime.setText("训练周期:"+indexEntity.getCycle()+"小时");
        holder.mTvRcmdNum.setText(indexEntity.getLearning_count()+"人学过");
        holder.ratingbarId.setCountSelected(indexEntity.getDifficulty());
        Glide.with(getContext()).load(indexEntity.getCover()).asBitmap().into(holder.mBgIvRcmd);
        holder.mBgIvRcmd.setAlpha(0.7f);
    }

    public void setEditMode(int editMode) {
        mEditMode = editMode;
        notifyDataSetChanged();
    }


    public void getPosition(int selectPosition,Boolean isShow) {
        this.selectPosition = selectPosition;
        this.isShow = isShow;
    }


    class CollectHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.bg_iv_rcmd)
        ImageView mBgIvRcmd;
        @Bind(R.id.tv_rcmd_title)
        TextView mTvRcmdTitle;
        @Bind(R.id.tv_rcmd_free)
        TextView mTvRcmdFree;
        @Bind(R.id.tv_rcmd_time)
        TextView mTvRcmdTime;
        @Bind(R.id.tv_rcmd_num)
        TextView mTvRcmdNum;
        @Bind(R.id.ratingbarId)
        XLHRatingBar ratingbarId;
        @Bind(R.id.iv_select_collect)
        ImageView mIvSelectCollect;

        public CollectHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
